<template>
  <div class="app-container">
      <div>
        <el-button type="success" @click="addmodel">添加</el-button>
      </div>
      <div class="tableClass">
        <el-table :data="formdata" v-loading="loading"  :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          border style="width: 100%;">
          <el-table-column align="center" prop="id" label="序号" />
          <el-table-column align="center" prop="title" label="名称" />
          <el-table-column align="center" prop="img_url" label="图片">
            <template slot-scope="scope">
                <el-button type="text" size="medium">
                  <img style="width: 50%;" :src="scope.row.img_url" />
                </el-button>
              </template>
          </el-table-column>
          <!--<el-table-column align="center" prop="classify_name" label="关联商品类型">
            <template slot-scope="scope">
                <div v-if="scope.row.classify_id==0">不选择</div>
                <div v-else>{{scope.row.classify_name}}</div>
              </template>
          </el-table-column>-->
          <!-- <el-table-column align="center" prop="website" label="关联商品类型" />
          <el-table-column align="center" prop="contact" label="关联商品数量" /> -->
          <el-table-column align="center" prop="sort" label="排序" />
          <el-table-column label="操作" align="center" width="280px">
            <template slot-scope="scope">
              <!-- scope.row 获取整行信息 表示当前单元格 #ff9900ba-->
              <el-button class="btnAppeal" size="small" type="warning" @click="updateMsg(scope.row.id)">修改</el-button>
              <el-button class="btnAppeal" size="small" type="danger" @click="delMsg(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <br>
        <!-- 分页 -->
        <div style="text-align: center;">
          <el-pagination background layout="prev, pager, next" @current-change="pageChange" :current-page="startpage" :page-size="pageSize"
            :total="totalusers" />
        </div>
      </div>
    <!-- </el-card> -->


    <!-- 添加模态款 -->
    <el-dialog style="width: 50%;margin: auto;" :close-on-click-modal="false" :title="title" :visible.sync="modelMsg">
      <div class="msgDiv">
        <!-- <h3>店铺基础信息</h3> -->
        <div>
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item v-show="vshow" label="序号">
              <el-input v-model="form.id" disabled></el-input>
            </el-form-item>
            <el-form-item label="名称" :required="true">
              <el-input v-model="form.title" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="排序">
              <el-input v-model="form.sort" placeholder="请输入顺序排序"></el-input>
            </el-form-item>
            <!-- 改版了, 不用这个了 -->
             <!--<el-form-item label="服务类型" :required="true" style="text-align: left;">
              <el-select v-model="form.classify_id" placeholder="请选择服务类型">
                <el-option v-for="item in typeList" :key="item.id" :label="item.label" :value="item.id" />
              </el-select>
            </el-form-item>-->
            <!-- 封面图片 -->

            <el-form-item label="链接">

              <el-autocomplete popper-class="my-autocomplete" v-model="form.link_url" :fetch-suggestions="querySearch"
                              placeholder="请输入内容" style="width: 100%" @select="handleSelects">
                <i class="el-icon-edit el-input__icon" slot="suffix" @click="handleIconClick"></i>
                <template slot-scope="{ item }">
                  <div class="name">{{ item.name }}</div>
                  <span class="addr">{{ item.link_url }}</span>
                </template>
              </el-autocomplete>


            </el-form-item>

            <el-form-item label="功能图片" :required="true" style="text-align: left;">
              <el-tooltip placement="top">
                <div slot="content">请上传功能图片</div>
                <i class="el-icon-info" style="font-size: 18px;"></i>
              </el-tooltip>
              <el-upload class="avatar-uploader" action="https://ai-tongue.dindong.cn/api/file/upload-img/" name='photo'
                :show-file-list="false" :on-success="handleAvatarSuccess">
                <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <!-- 封面图片end -->
          </el-form>
          <el-button type="success" @click="addMsg(form)">{{addORupdate}}</el-button>
        </div>
      </div>
    </el-dialog>

     </div>
</template>

<script>
  export default {
    data() {
      return {
        modelMsg: false,
        form: {
          id: "",
          title: "",
          sort: "",
          img_id: "",
          classify_id:"",
          imageUrl: "",
          link_url: "",
          level: "",      // 用来判断是跳到tabbar页, 还是子页面
        },
        vshow: false,
        loading: false,
        title: "",
        addORupdate: "", //添加或修改
        formdata: [],
        typeList:[],
        sectionUrlList: [],
        totalusers: 0, //总行数
        pageSize: 5, //每页几行
        startpage: 1 //起始页
      }
    },
    computed: {
      sectionUrl() {

      },
    },
    mounted() {
      this.getButtonList()
      this.getCourseTypeList()
      this.getSectionUrl()
    },
    methods: {

      querySearch(queryString, cb) {
      var restaurants = this.sectionUrlList;
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(restaurants);
    },

      createFilter(queryString) {
        return (restaurant) => {
        return (restaurant.link_url.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },


      //获取课程类型列表
      getCourseTypeList() {
        this.typeList = []
        // let tempObj = {
        //   label: '不选择',
        //   id: 0
        // }
        //  this.typeList.unshift(tempObj)
        let params = {
          "page": 1,
          "limit": 100000
        }
        this.$api.getCourseTypeList(params).then(res => {
          res.data.data.forEach(item => {
            let option = {
              "id": item.id,
              "label": item.name
            }
            this.typeList.push(option)
          })
        })
      },
      //获取分页列表
      getButtonList() {

        let params = {
          "page": this.startpage,
          "limit": this.pageSize,
        }
        //调用后台提供的接口方法
        this.$api.getButtonList(params).then(res => {
          if(res.data.level=="success"){
            this.formdata = res.data.data
            this.totalusers = res.data.page_info.total
            this.loading = false
          }else{
            this.$message.error(res.data.message)
          }

        })
      },
      //分页 pageChange当前页发生改变触发函数
      async pageChange(page) {
        this.startpage = page
        this.loading = true
        //调用获取列表的方法
        this.getButtonList()
      },
      addmodel() {
        this.modelMsg = true
        this.title = '添加功能区'
        this.addORupdate = "添加",
          this.vshow = false
        this.form.id="",
        this.form.title= "",
        this.form.sort= "",
        this.form.classify_id="",
        this.form.img_id= "",
        this.form.imageUrl= ""
      },
      //提交店铺入驻申请
      async addMsg(form) {

        //判断带星号的是否为空
       if ((form.title  && form.img_id && this.form.link_url) == "") {
          this.$message({
            message: "带星号为必填项，请完善信息！",
            type: "warning"
          })
          return false;
        }

          /* 显示加载中。。。 */
          const loadingOne = this.$loading({
            lock: true,
            text: '保存中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })

          if (form.id > 0) {
            /* 修改 */
            await this.$api.setButton(form).then(res => {
              console.log(res)
              this.$message({
                type: res.data.level,
                message: res.data.message
              });
              if (res.data.level == "success") {
                this.getButtonList()
                loadingOne.close()
                this.modelMsg = false
              } else {
                this.modelMsg = true
                loadingOne.close()
              }

            })
          } else {
            /* 添加 */
            await this.$api.setButton(form).then(res => {
              this.$message({
                type: res.data.level,
                message: res.data.message
              });
              if (res.data.level == "success") {
                this.getButtonList()
                loadingOne.close()
                this.modelMsg = false
              } else {
                this.modelMsg = true
                loadingOne.close()
              }

            })
          }
          this.modelMsg = false
      },
      //修改弹出框
      updateMsg(id) {
        this.modelMsg = true
        this.title = '修改功能区'
        this.addORupdate = "修改",
        this.vshow = true
        let dataid = {
          "id":id
        }
        this.$api.getButtonDetail(dataid).then(res=>{
          console.log(res.data.data)
          let data = res.data.data
          this.form.id = data.id,
          this.form.title = data.title,
          this.form.sort = data.sort,
          this.form.img_id = data.img_id,
          this.form.imageUrl = data.img_url,
          this.form.link_url = data.link_url,
          this.form.level = data.level,
          this.form.classify_id =parseInt(data.classify_id)
        })

      },
      //删除店铺信息
      async delMsg(id) {
        let delid = {
          id: id
        }
        await this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {

          this.$api.delButton(delid).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            if (res.data.level == "success") {
              this.getButtonList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //图片上传
      handleAvatarSuccess(res, file) {
        //console.log(res.data.id)
        this.form.img_id = res.data.id
        //console.log(file)
        this.form.imageUrl = res.data.img_url;
      },

      // 获取小程序链接路径列表
      async getSectionUrl() {
        const res = await this.$api.getSectionUrl();
        if (res.data.level === 'success') {
          this.sectionUrlList = res.data.data;
        }else {
          this.$message.error(res.data.message);
        }
      },
      handleSelects(item){
        this.form.link_url = item.link_url;
        this.form.level = item.level;
      },
      handleIconClick(ev) {
      // console.log(ev);
      }

    }


  }
</script>
<style scoped="scoped">
  /deep/.el-card__header {
    background-color: #409EFF;
    color: whitesmoke;
  }

  /deep/.el-dialog {
    margin-top: 10vh !important;
    width: 60%;
  }

  .msgDiv {
    width: 80%;
    margin: auto;
    text-align: center;
  }

  .el-input {
    display: flex;
    flex-grow: <2>;
  }

  /* 图片上传css */
  .avatar-uploader /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }

  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
